﻿package game.swfloader {
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.display.MovieClip;
	
	public class SWFLoader extends Loader {
		
		public var progress:Number = 0;
		
		public function SWFLoader():void {
			
			this.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,	progressHandler);
			this.contentLoaderInfo.addEventListener(Event.COMPLETE,			completeHandler);
			this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,	errorHandler);
			
		}
		
		public function set (url:String):void {
			try { super.close(); } catch (e:Error) { }
			super.unload();
			super.load( new URLRequest (url), new LoaderContext(true) );
		}
		
		private function completeHandler(event:Event):void { 
			this.dispatchEvent( new SWFLoaderEvent ( SWFLoaderEvent.LOADED, this.content as MovieClip ) );
		}
		
		private function errorHandler(event:IOErrorEvent):void { trace (event);
			this.dispatchEvent( new SWFLoaderEvent ( SWFLoaderEvent.ERROR, null ) );
		}
		
		private function progressHandler(event:ProgressEvent):void {
			progress = event.bytesLoaded/event.bytesTotal;
			
			this.dispatchEvent( new ProgressEvent ( ProgressEvent.PROGRESS ) );
		}
		
	}
	
}